package org.shanktech.tshare.binlogsync.process.transfer;

import org.shanktech.tshare.binlogsync.dao.version2.generated.UserAddressVersion2Mapper;
import org.shanktech.tshare.binlogsync.dao.version2.generated.UserInfoVersion2Mapper;
import org.shanktech.tshare.binlogsync.dao.version2.model.generated.UserAddress;
import org.shanktech.tshare.binlogsync.dao.version2.model.generated.UserAddressExample;
import org.shanktech.tshare.binlogsync.dao.version2.model.generated.UserInfo;
import org.shanktech.tshare.binlogsync.model.binlog.BinlogData;
import org.shanktech.tshare.binlogsync.process.AbstractTableTransfer;
import org.shanktech.tshare.binlogsync.util.SpringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;


public class UserInfoTransfer extends AbstractTableTransfer {

  private UserInfoVersion2Mapper userInfoVersion2Mapper;

  private UserAddressVersion2Mapper userAddressVersion2Mapper;

  public UserInfoTransfer() {
    userInfoVersion2Mapper =
        (UserInfoVersion2Mapper) SpringUtil.getBean("userInfoVersion2Mapper");
    userAddressVersion2Mapper = (UserAddressVersion2Mapper) SpringUtil
        .getBean("userAddressVersion2Mapper");
  }

  //  由于时间的关系 这边只对ROW模式进行了处理，没有对mixed进行处理，大家可以自己进行扩展。
  @Override
  protected void insert(BinlogData binlogData, Object object) {
    org.shanktech.tshare.binlogsync.dao.version1.model.generated.UserInfo userInfoVersion1 =
        (org.shanktech.tshare.binlogsync.dao.version1.model.generated.UserInfo) object;
    
    UserInfo userInfoVersion2 = new UserInfo();
    BeanUtils.copyProperties(userInfoVersion1, userInfoVersion2);
    
    UserAddress userAddressVersion2 = new UserAddress();
    userAddressVersion2.setUserId(userInfoVersion1.getId());
    userAddressVersion2.setAddress(userInfoVersion1.getAdddress());
    if (null != userInfoVersion2Mapper && null != userAddressVersion2Mapper) {
      userInfoVersion2Mapper.insert(userInfoVersion2);
      userAddressVersion2Mapper.insert(userAddressVersion2);
    }
  }

  @Override
  protected void update(BinlogData binlogData, Object afterObject, Object beforObject) {
    org.shanktech.tshare.binlogsync.dao.version1.model.generated.UserInfo userInfoVersion1After =
        (org.shanktech.tshare.binlogsync.dao.version1.model.generated.UserInfo) afterObject;
    
    org.shanktech.tshare.binlogsync.dao.version1.model.generated.UserInfo userInfoVersion1befor =
        (org.shanktech.tshare.binlogsync.dao.version1.model.generated.UserInfo) beforObject;
    
    UserInfo userInfoVersion2 = new UserInfo();
    BeanUtils.copyProperties(userInfoVersion1After, userInfoVersion2);
    
    if (null != userInfoVersion2Mapper && null != userAddressVersion2Mapper) {
      userInfoVersion2Mapper.updateByPrimaryKeySelective(userInfoVersion2);
      
      // 先删除原先的，然后再插入更新的
      UserAddressExample example = new UserAddressExample();
      example.createCriteria().andUserIdEqualTo(userInfoVersion1After.getId());
      example.createCriteria().andAddressEqualTo(userInfoVersion1befor.getAdddress());
      
      userAddressVersion2Mapper.deleteByExample(example);
      
      UserAddress userAddressVersion2 = new UserAddress();
      userAddressVersion2.setUserId(userInfoVersion1After.getId());
      userAddressVersion2.setAddress(userInfoVersion1After.getAdddress());
      userAddressVersion2Mapper.insert(userAddressVersion2);
    }
    // 从表设计来看，v2.0.0版本是要支持多个地址的，所以在更新之前要先进行查询一下
  }

  @Override
  @Transactional
  protected void delete(BinlogData binlogData, Object object) {
    org.shanktech.tshare.binlogsync.dao.version1.model.generated.UserInfo userInfoVersion1 =
        (org.shanktech.tshare.binlogsync.dao.version1.model.generated.UserInfo) object;
    UserAddressExample example = new UserAddressExample();
    example.createCriteria().andUserIdEqualTo(userInfoVersion1.getId());
    userInfoVersion2Mapper.deleteByPrimaryKey(userInfoVersion1.getId());
    userAddressVersion2Mapper.deleteByExample(example);
  }
  
  public UserInfoVersion2Mapper getUserInfoVersion2Mapper() {
    return userInfoVersion2Mapper;
  }

  public void setUserInfoVersion2Mapper(
      UserInfoVersion2Mapper userInfoVersion2Mapper) {
    this.userInfoVersion2Mapper = userInfoVersion2Mapper;
  }

  public UserAddressVersion2Mapper getUserAddressVersion2Mapper() {
    return userAddressVersion2Mapper;
  }

  public void setUserAddressVersion2Mapper(
      UserAddressVersion2Mapper userAddressVersion2Mapper) {
    this.userAddressVersion2Mapper = userAddressVersion2Mapper;
  }

}
